﻿@page "/product/update/{id:guid}"
@using BlazorCRUD.App.Models
@using System.Text.Json
@rendermode InteractiveServer
@inject IToastService toastService
@inject IHttpClientFactory httpClientFactory
@inject NavigationManager navigationManager

<h3>Update Product</h3>

<EditForm Model="productModel" FormName="UpdateProduct" OnValidSubmit="UpdateProduct">
    <DataAnnotationsValidator />

    <div class="container">
        <div class="row mb-3">
            <div class="col-md-6">
                <label>Name</label>
                <InputText class="form-control" @bind-Value="productModel.Name" />
                <ValidationMessage For="()=> productModel.Name" />
            </div>
            <div class="col-md-6">
                <label>Quantity</label>
                <InputNumber class="form-control" @bind-Value="productModel.Quantity" />
                <ValidationMessage For="()=> productModel.Quantity" />
            </div>
        </div>

        <div class="row mb-3">
            <div class="col-md-6">
                <label>Price</label>
                <InputNumber class="form-control" @bind-Value="productModel.Price" />
                <ValidationMessage For="()=> productModel.Price" />
            </div>
            <div class="col-md-6">
                <label>Description</label>
                <InputText class="form-control" @bind-Value="productModel.Description" />
                <ValidationMessage For="()=> productModel.Description" />
            </div>
        </div>
        <button class="btn btn-secondary" type="submit">Save</button>
    </div>
</EditForm>

@code {
    [Parameter]
    public Guid id { get; set; }
    private UpdateProductModel productModel = new UpdateProductModel();

    protected override async Task OnInitializedAsync()
    {
        var httpClient = httpClientFactory.CreateClient();

        var httpResponse = await httpClient.GetAsync($"https://localhost:7277/Product/{id}");
        if (httpResponse.IsSuccessStatusCode)
        {
            var responseContent = await httpResponse.Content.ReadAsStringAsync();
            productModel = JsonSerializer.Deserialize<UpdateProductModel>(responseContent, new JsonSerializerOptions()
                {
                    PropertyNameCaseInsensitive = true
                });
        }
    }

    public async Task UpdateProduct()
    {
        var httpClient = httpClientFactory.CreateClient();

        var jsonContent = JsonSerializer.Serialize(productModel);
        var stringContent = new StringContent(jsonContent, System.Text.Encoding.UTF8, "application/json");
        var httpResponse = await httpClient.PutAsync($"https://localhost:7277/Product/{id}", stringContent);
        if (httpResponse.IsSuccessStatusCode)
        {
            var responseContent = await httpResponse.Content.ReadAsStringAsync();
            var productModel = JsonSerializer.Deserialize<ProductModel>(responseContent, new JsonSerializerOptions()
                {
                    PropertyNameCaseInsensitive = true
                });
            if (productModel != null)
            {
                toastService.ShowSuccess("Update product successfully");
                navigationManager.NavigateTo("/product");
            }
        }
    }
}
